package top.yuzt.animalserver.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.yuzt.animalserver.entity.Animal;
import top.yuzt.animalserver.entity.User;
import top.yuzt.animalserver.mapper.AnimalMapper;
import top.yuzt.animalserver.service.impl.AnimalServiceImpl;
import top.yuzt.animalserver.service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuzt
 * @since 2024-05-16
 */
@RestController
@RequestMapping("/animal")
public class    AnimalController {
    @Autowired
    private AnimalServiceImpl animalService;

    @Autowired
    private AnimalMapper animalMapper;
    @Autowired
    private UserServiceImpl userService;

    @SaIgnore
    @GetMapping("/getlist")
    public List<Animal> getuserlist(){

        return animalService.list();
    }


    //通过用户id获取此用户管理的动物
    @SaIgnore
    @GetMapping("/getlist/byuseridfilter")
    public List<Animal> getUserListByUserIdisNull(@RequestParam Integer id) {
        if(Integer.parseInt(userService.getById(id).getRole()) > 2){
            return animalMapper.ListisNull();
        }else {
            return animalMapper.findByUseridisNull(id);
        }
    }
    @SaIgnore
    @GetMapping("/getlist/byuserid")
    public List<Animal> getUserListByUserId(@RequestParam Integer id) {
        if(Integer.parseInt(userService.getById(id).getRole()) > 2){
            return getuserlist();
        }else {
            return animalMapper.findByUserid(id);
        }
    }
    //新增
    @SaIgnore
    @PostMapping("/save")
    public boolean saveanimal(@RequestBody Animal animal){
        return animalService.save(animal);
    }
    //更新
    @SaIgnore
    @PostMapping("/update")
    public boolean updateanimal(@RequestBody Animal animal){
        return  animalService.updateById(animal);
    }
    //删除
    @SaIgnore
    @PostMapping("/remove")
    public boolean removeanimal(@RequestBody Animal animal){
        return  animalService.removeById(animal.getId());
    }
    //通过id查找
    @SaIgnore
    @GetMapping("/find")
    public Animal findanimalByID(Integer id){
        return animalService.getById(id);
    }

    @SaIgnore
    @GetMapping("/gettype")
    public List<String> gettypelist(){
        List<String> strings = new ArrayList<>();
        for (Animal animal : animalService.list()){
            if(!strings.contains(animal.getType())){
                strings.add(animal.getType());
            }
        }
        return strings;
    }


    @GetMapping("/getwarning")
    public List<Animal> getwaAnimal(){
        return animalMapper.findByHealth("3");
    }
}
